package com.ocm.tourism.core.model;

import java.util.ArrayList;
import java.util.List;

import com.ocm.tourism.core.util.ValueUtil;

public class HotelCriteria implements SearchCriteria {

   private String name;
   
   private String address;
   
   private String mobilephone;
   
   private String homephone;
   
   private Double priceFrom;
   
   private Double priceTo;

   private FieldCriteria nameFieldCriteria;
   
   private FieldCriteria addressFieldCriteria;
   
   private FieldCriteria mobilephoneFieldCriteria;
   
   private FieldCriteria homephoneFieldCriteria;
   
   private FieldCriteria priceFromFieldCriteria;
   
   private FieldCriteria priceToFieldCriteria;
   
   private List<FieldCriteria> fieldCriterias = new ArrayList<FieldCriteria>();

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
      if (!ValueUtil.isEmpty(name)){
         if (nameFieldCriteria == null){
            nameFieldCriteria = new FieldCriteria();
            this.fieldCriterias.add(nameFieldCriteria);
         }
         nameFieldCriteria.setName("name");
         nameFieldCriteria.setValue(name+"%");
         nameFieldCriteria.setComparisonType(ComparisonType.LIKE);
      }
   }

   public String getAddress() {
      return address;
   }

   public void setAddress(String address) {
      this.address = address;
      if (!ValueUtil.isEmpty(address)){
         if (addressFieldCriteria == null){
            addressFieldCriteria = new FieldCriteria();
            this.fieldCriterias.add(addressFieldCriteria);
         }
         addressFieldCriteria.setName("address");
         addressFieldCriteria.setValue("%"+address+"%");
         addressFieldCriteria.setComparisonType(ComparisonType.LIKE);
      }
   }

   public String getMobilephone() {
      return mobilephone;
   }

   public void setMobilephone(String mobilephone) {
      this.mobilephone = mobilephone;
      if (!ValueUtil.isEmpty(mobilephone)){
         if (mobilephoneFieldCriteria == null){
            mobilephoneFieldCriteria = new FieldCriteria();
            this.fieldCriterias.add(mobilephoneFieldCriteria);
         }
         mobilephoneFieldCriteria.setName("mobilephone");
         mobilephoneFieldCriteria.setValue(mobilephone);
         mobilephoneFieldCriteria.setComparisonType(ComparisonType.EQUAL);
      }
   }

   public String getHomephone() {
      return homephone;
   }

   public void setHomephone(String homephone) {
      this.homephone = homephone;
      if (!ValueUtil.isEmpty(homephone)){
         if (homephoneFieldCriteria == null){
            homephoneFieldCriteria = new FieldCriteria();
            this.fieldCriterias.add(homephoneFieldCriteria);
         }
         homephoneFieldCriteria.setName("homephone");
         homephoneFieldCriteria.setValue(homephone);
         homephoneFieldCriteria.setComparisonType(ComparisonType.EQUAL);
      }
   }

   public Double getPriceFrom() {
      return priceFrom;
   }

   public void setPriceFrom(Double priceFrom) {
      this.priceFrom = priceFrom;
      if (!ValueUtil.isNull(priceFrom)){
         if (priceFromFieldCriteria == null){
            priceFromFieldCriteria = new FieldCriteria();
            this.fieldCriterias.add(priceFromFieldCriteria);
         }
         priceFromFieldCriteria.setName("priceFrom");
         priceFromFieldCriteria.setValue(priceFrom);
         priceFromFieldCriteria.setComparisonType(ComparisonType.GREATER_EQUAL);
      }
   }

   public Double getPriceTo() {
      return priceTo;
   }

   public void setPriceTo(Double priceTo) {
      this.priceTo = priceTo;
      if (!ValueUtil.isNull(priceTo)){
         if (priceToFieldCriteria == null){
            priceToFieldCriteria = new FieldCriteria();
            this.fieldCriterias.add(priceToFieldCriteria);
         }
         priceToFieldCriteria.setName("priceTo");
         priceToFieldCriteria.setValue(priceTo);
         priceToFieldCriteria.setComparisonType(ComparisonType.SMALLER_EQUAL);
      }
   }

   @Override
   public List<FieldCriteria> getFieldCriterias() {
      return this.fieldCriterias;
   }

   @Override
   public boolean hasFieldCriteria(String fieldName) {
      for (FieldCriteria fieldCriteria : this.fieldCriterias){
         String namefieldCriteria = fieldCriteria.getName();
         if (namefieldCriteria.equals(fieldName) || namefieldCriteria.equals(fieldName)){
            return true;
         }
      }
      return false;
   }

   public HotelCriteria fill(){
      setAddress(this.address);
      setHomephone(this.homephone);
      setMobilephone(this.homephone);
      setName(this.name);
      setPriceFrom(this.priceFrom);
      setPriceTo(this.priceTo);
      return this;
   }
}
